package hut.game.table;

import hut.game.GameActivity;
import hut.game.R;
import hut.game.actor.Computer;
import hut.game.actor.Player;
import hut.game.unit.Chot;
import hut.game.unit.Ma;
import hut.game.unit.Phao;
import hut.game.unit.QuanCo;
import hut.game.unit.Sy;
import hut.game.unit.Tinh;
import hut.game.unit.Xe;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

public class VanCo {

	// Cac quan bi an
	public static ArrayList<QuanCo> QuanDoBiAn = new ArrayList<QuanCo>();
	public static ArrayList<QuanCo> QuanDenBiAn = new ArrayList<QuanCo>();
	// -------------------
	public static boolean state = false; // Bien xac dinh che do choi PvsP = true or PvsComputer = false
	public static boolean resume = false;
	public static int winner = 2;
	public static Computer computer = null;
	public static Player n1 = null;
	public static Player n2 = null;
	public static boolean DangChoi = false;
	public static boolean Finish = false;
	public static int LuotDi = 0;
	public static int ply = 0;
	public static boolean Marked = false; // Kiem tra xem quan co co duoc chon hay ko
	public static QuanCo DanhDau; // Tham chieu den quan co duoc chon trong 1 nuoc di
	public static ArrayList<NuocDi> GameLog = new ArrayList<NuocDi>(); // Luu cac nuoc di
	public static int turn = 0;// Luu tong so luot di cua van co - dung cho AI
	// --------------------- Method ResetVanCo

	public static void ResetVanCo() {
		if (VanCo.state) {
			if(n1!=null && n2!=null){
				n1.ResetPlayer();
				n2.ResetPlayer();
			}
		} else {
			if(n1!=null && computer!=null){
				n1.ResetPlayer();
				computer.ResetComputer();
			}
		}
		state = false;
		winner = 2;
		LuotDi = 0;
		DangChoi = false;
		DanhDau = new QuanCo();
		GameLog.removeAll(GameLog);
		ply = 0;
		turn = 0;
		Marked = false;
		QuanDenBiAn.removeAll(QuanDenBiAn);
		QuanDoBiAn.removeAll(QuanDoBiAn);
	}

	// --------------------- Method OCoTrong
	public static void OCoTrong(int i, int j) {
		BanCo.ViTri[i][j].Trong = true;
		BanCo.ViTri[i][j].Ten = "";
		BanCo.ViTri[i][j].ThuTu = "";
		BanCo.ViTri[i][j].Phe = 2;
		BanCo.ViTri[i][j].TrangThai = 0;
	}

	// --------------------- Method ChieuTuong
	public static boolean ChieuTuong(QuanCo tuong) {
		boolean chieu = false;
		int xe0 = 0, xe1 = 0, phao0 = 0, phao1 = 0, ma0 = 0, ma1 = 0, chot0 = 0, chot1 = 0, chot2 = 0, chot3 = 0, chot4 = 0,
			sy0 = 0, sy1 = 0, tinh0 = 0, tinh1 = 0;
		if (VanCo.state) {
			if (tuong.Phe == VanCo.n1.Phe) {
				if (VanCo.n2.qXe[0].TrangThai == 1) if (VanCo.n2.qXe[0].KiemTra(tuong.Hang, tuong.Cot) == 1) xe0 = 1;
				if (VanCo.n2.qXe[1].TrangThai == 1) if (VanCo.n2.qXe[1].KiemTra(tuong.Hang, tuong.Cot) == 1) xe1 = 1;
				if (VanCo.n2.qPhao[0].TrangThai == 1) if (VanCo.n2.qPhao[0].KiemTra(tuong.Hang, tuong.Cot) == 1) phao0 = 1;
				if (VanCo.n2.qPhao[1].TrangThai == 1) if (VanCo.n2.qPhao[1].KiemTra(tuong.Hang, tuong.Cot) == 1) phao1 = 1;
				if (VanCo.n2.qSy[0].TrangThai == 1)	if (VanCo.n2.qSy[0].KiemTra(tuong.Hang, tuong.Cot) == 1) sy0 = 1;
				if (VanCo.n2.qSy[1].TrangThai == 1)	if (VanCo.n2.qSy[1].KiemTra(tuong.Hang, tuong.Cot) == 1) sy1 = 1;
				if (VanCo.n2.qTinh[0].TrangThai == 1) if (VanCo.n2.qTinh[0].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
				if (VanCo.n2.qTinh[1].TrangThai == 1) if (VanCo.n2.qTinh[1].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh1 = 1;
				if (VanCo.n2.qMa[0].TrangThai == 1) if (VanCo.n2.qMa[0].KiemTra(tuong.Hang, tuong.Cot) == 1) ma0 = 1;
				if (VanCo.n2.qMa[1].TrangThai == 1)	if (VanCo.n2.qMa[1].KiemTra(tuong.Hang, tuong.Cot) == 1) ma1 = 1;
				if (VanCo.n2.qChot[0].TrangThai == 1) if (VanCo.n2.qChot[0].KiemTra(tuong.Hang, tuong.Cot) == 1) chot0 = 1;
				if (VanCo.n2.qChot[1].TrangThai == 1) if (VanCo.n2.qChot[1].KiemTra(tuong.Hang, tuong.Cot) == 1) chot1 = 1;
				if (VanCo.n2.qChot[2].TrangThai == 1) if (VanCo.n2.qChot[2].KiemTra(tuong.Hang, tuong.Cot) == 1) chot2 = 1;
				if (VanCo.n2.qChot[3].TrangThai == 1) if (VanCo.n2.qChot[3].KiemTra(tuong.Hang, tuong.Cot) == 1) chot3 = 1;
				if (VanCo.n2.qChot[4].TrangThai == 1) if (VanCo.n2.qChot[4].KiemTra(tuong.Hang, tuong.Cot) == 1) chot4 = 1;
			} else {
				if (VanCo.n1.qXe[0].TrangThai == 1) if (VanCo.n1.qXe[0].KiemTra(tuong.Hang, tuong.Cot) == 1) xe0 = 1;
				if (VanCo.n1.qXe[1].TrangThai == 1)	if (VanCo.n1.qXe[1].KiemTra(tuong.Hang, tuong.Cot) == 1) xe1 = 1;
				if (VanCo.n1.qPhao[0].TrangThai == 1) if (VanCo.n1.qPhao[0].KiemTra(tuong.Hang, tuong.Cot) == 1) phao0 = 1;
				if (VanCo.n1.qPhao[1].TrangThai == 1) if (VanCo.n1.qPhao[1].KiemTra(tuong.Hang, tuong.Cot) == 1) phao1 = 1;
				if (VanCo.n1.qSy[0].TrangThai == 1)	if (VanCo.n1.qSy[0].KiemTra(tuong.Hang, tuong.Cot) == 1) sy0 = 1;
				if (VanCo.n1.qSy[1].TrangThai == 1)	if (VanCo.n1.qSy[1].KiemTra(tuong.Hang, tuong.Cot) == 1) sy1 = 1;
				if (VanCo.n1.qTinh[0].TrangThai == 1) if (VanCo.n1.qTinh[0].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
				if (VanCo.n1.qTinh[1].TrangThai == 1) if (VanCo.n1.qTinh[1].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh1 = 1;
				if (VanCo.n1.qMa[0].TrangThai == 1) if (VanCo.n1.qMa[0].KiemTra(tuong.Hang, tuong.Cot) == 1) ma0 = 1;
				if (VanCo.n1.qMa[1].TrangThai == 1)	if (VanCo.n1.qMa[1].KiemTra(tuong.Hang, tuong.Cot) == 1) ma1 = 1;
				if (VanCo.n1.qChot[0].TrangThai == 1) if (VanCo.n1.qChot[0].KiemTra(tuong.Hang, tuong.Cot) == 1) chot0 = 1;
				if (VanCo.n1.qChot[1].TrangThai == 1) if (VanCo.n1.qChot[1].KiemTra(tuong.Hang, tuong.Cot) == 1) chot1 = 1;
				if (VanCo.n1.qChot[2].TrangThai == 1) if (VanCo.n1.qChot[2].KiemTra(tuong.Hang, tuong.Cot) == 1) chot2 = 1;
				if (VanCo.n1.qChot[3].TrangThai == 1) if (VanCo.n1.qChot[3].KiemTra(tuong.Hang, tuong.Cot) == 1) chot3 = 1;
				if (VanCo.n1.qChot[4].TrangThai == 1) if (VanCo.n1.qChot[4].KiemTra(tuong.Hang, tuong.Cot) == 1) chot4 = 1;
			}
		} else {
			if (tuong.Phe == VanCo.n1.Phe) {
				if (VanCo.computer.qXe[0].TrangThai == 1) if (VanCo.computer.qXe[0].KiemTra(tuong.Hang, tuong.Cot) == 1)xe0 = 1;
				if (VanCo.computer.qXe[1].TrangThai == 1) if (VanCo.computer.qXe[1].KiemTra(tuong.Hang, tuong.Cot) == 1)xe1 = 1;
				if (VanCo.computer.qPhao[0].TrangThai == 1) if (VanCo.computer.qPhao[0].KiemTra(tuong.Hang, tuong.Cot) == 1)phao0 = 1;
				if (VanCo.computer.qPhao[1].TrangThai == 1)	if (VanCo.computer.qPhao[1].KiemTra(tuong.Hang, tuong.Cot) == 1)phao1 = 1;
				if (VanCo.computer.qSy[0].TrangThai == 1) if (VanCo.computer.qSy[0].KiemTra(tuong.Hang, tuong.Cot) == 1) sy0 = 1;
				if (VanCo.computer.qSy[1].TrangThai == 1) if (VanCo.computer.qSy[1].KiemTra(tuong.Hang, tuong.Cot) == 1)sy1 = 1;
				if (VanCo.computer.qTinh[0].TrangThai == 1) if (VanCo.computer.qTinh[0].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
				if (VanCo.computer.qTinh[1].TrangThai == 1) if (VanCo.computer.qTinh[1].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh1 = 1;
				if (VanCo.computer.qMa[0].TrangThai == 1) if (VanCo.computer.qMa[0].KiemTra(tuong.Hang, tuong.Cot) == 1)ma0 = 1;
				if (VanCo.computer.qMa[1].TrangThai == 1) if (VanCo.computer.qMa[1].KiemTra(tuong.Hang, tuong.Cot) == 1)ma1 = 1;
				if (VanCo.computer.qChot[0].TrangThai == 1) if (VanCo.computer.qChot[0].KiemTra(tuong.Hang, tuong.Cot) == 1)chot0 = 1;
				if (VanCo.computer.qChot[1].TrangThai == 1)	if (VanCo.computer.qChot[1].KiemTra(tuong.Hang, tuong.Cot) == 1)chot1 = 1;
				if (VanCo.computer.qChot[2].TrangThai == 1)	if (VanCo.computer.qChot[2].KiemTra(tuong.Hang, tuong.Cot) == 1)chot2 = 1;
				if (VanCo.computer.qChot[3].TrangThai == 1)	if (VanCo.computer.qChot[3].KiemTra(tuong.Hang, tuong.Cot) == 1)chot3 = 1;
				if (VanCo.computer.qChot[4].TrangThai == 1)	if (VanCo.computer.qChot[4].KiemTra(tuong.Hang, tuong.Cot) == 1)chot4 = 1;
			} else {
				if (VanCo.n1.qXe[0].TrangThai == 1) if (VanCo.n1.qXe[0].KiemTra(tuong.Hang, tuong.Cot) == 1)xe0 = 1;
				if (VanCo.n1.qXe[1].TrangThai == 1)	if (VanCo.n1.qXe[1].KiemTra(tuong.Hang, tuong.Cot) == 1)xe1 = 1;
				if (VanCo.n1.qPhao[0].TrangThai == 1) if (VanCo.n1.qPhao[0].KiemTra(tuong.Hang, tuong.Cot) == 1)phao0 = 1;
				if (VanCo.n1.qPhao[1].TrangThai == 1) if (VanCo.n1.qPhao[1].KiemTra(tuong.Hang, tuong.Cot) == 1)phao1 = 1;
				if (VanCo.n1.qSy[0].TrangThai == 1) if (VanCo.n1.qSy[0].KiemTra(tuong.Hang, tuong.Cot) == 1) sy0 = 1;
				if (VanCo.n1.qSy[1].TrangThai == 1) if (VanCo.n1.qSy[1].KiemTra(tuong.Hang, tuong.Cot) == 1)sy1 = 1;
				if (VanCo.n1.qTinh[0].TrangThai == 1) if (VanCo.n1.qTinh[0].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh0 = 1;
				if (VanCo.n1.qTinh[1].TrangThai == 1) if (VanCo.n1.qTinh[1].KiemTra(tuong.Hang, tuong.Cot) == 1) tinh1 = 1;
				if (VanCo.n1.qMa[0].TrangThai == 1) if (VanCo.n1.qMa[0].KiemTra(tuong.Hang, tuong.Cot) == 1)ma0 = 1;
				if (VanCo.n1.qMa[1].TrangThai == 1) if (VanCo.n1.qMa[1].KiemTra(tuong.Hang, tuong.Cot) == 1)ma1 = 1;
				if (VanCo.n1.qChot[0].TrangThai == 1) if (VanCo.n1.qChot[0].KiemTra(tuong.Hang, tuong.Cot) == 1)chot0 = 1;
				if (VanCo.n1.qChot[1].TrangThai == 1) if (VanCo.n1.qChot[1].KiemTra(tuong.Hang, tuong.Cot) == 1)chot1 = 1;
				if (VanCo.n1.qChot[2].TrangThai == 1) if (VanCo.n1.qChot[2].KiemTra(tuong.Hang, tuong.Cot) == 1)chot2 = 1;
				if (VanCo.n1.qChot[3].TrangThai == 1) if (VanCo.n1.qChot[3].KiemTra(tuong.Hang, tuong.Cot) == 1)chot3 = 1;
				if (VanCo.n1.qChot[4].TrangThai == 1) if (VanCo.n1.qChot[4].KiemTra(tuong.Hang, tuong.Cot) == 1)chot4 = 1;
			}
		}
		if (xe0 != 1 && xe1 != 1 && phao0 != 1 && phao1 != 1 && ma0 != 1
				&& ma1 != 1 && chot0 != 1 && chot1 != 1 && chot2 != 1
				&& chot3 != 1 && chot4 != 1 && sy0 != 1 && sy1 != 1 && tinh0 != 1 && tinh1 != 1)
			chieu = false;
		else
			chieu = true;
		return chieu;
	}

	// --------------------- Method KiemTraChieuTuong
	public static void KiemTraChieuTuong() {
		int t = 0;
		if (state) {
			if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true && VanCo.ChieuTuong(VanCo.n2.qTuong) == false) t = 0;
			if (VanCo.ChieuTuong(VanCo.n1.qTuong) == false && VanCo.ChieuTuong(VanCo.n2.qTuong) == true) t = 1;
			if (VanCo.ChieuTuong(VanCo.n1.qTuong) == false && VanCo.ChieuTuong(VanCo.n2.qTuong) == false) t = 2;
		} else {
			if (VanCo.ChieuTuong(VanCo.n1.qTuong) == true && VanCo.ChieuTuong(VanCo.computer.qTuong) == false)t = 0;
			if (VanCo.ChieuTuong(VanCo.n1.qTuong) == false && VanCo.ChieuTuong(VanCo.computer.qTuong) == true)	t = 1;
			if (VanCo.ChieuTuong(VanCo.n1.qTuong) == false && VanCo.ChieuTuong(VanCo.computer.qTuong) == false)	t = 2;
		}
		switch (t) {
		case 0:
			if (VanCo.state) {
				VanCo.n1.qTuong.imgUnit.setImageResource(R.drawable.rtuong_bichieu);
			} else {
				VanCo.n1.qTuong.imgUnit.setImageResource(R.drawable.btuong_bichieu);
			}
			if (VanCo.winner == 2) {
				Toast.makeText(GameActivity.gameContext, "Check",
						Toast.LENGTH_SHORT).show();
			}
			// if (VanCo.AmThanh) ClickSound("chieu");
			break;
		case 1:
			if (VanCo.state) {
				VanCo.n2.qTuong.imgUnit.setImageResource(R.drawable.btuong_bichieu);
			} else {
				VanCo.computer.qTuong.imgUnit.setImageResource(R.drawable.rtuong_bichieu);
			}
			if (VanCo.winner == 2) {
				Toast.makeText(GameActivity.gameContext, "Check",
						Toast.LENGTH_SHORT).show();
			}
			// if (VanCo.AmThanh) ClickSound("chieu");
			break;
		case 2:
			if (VanCo.state) {
				VanCo.n1.qTuong.imgUnit.setImageResource(R.drawable.rtuong);
				VanCo.n2.qTuong.imgUnit.setImageResource(R.drawable.btuong);
			} else {
				VanCo.computer.qTuong.imgUnit.setImageResource(R.drawable.rtuong);
				VanCo.n1.qTuong.imgUnit.setImageResource(R.drawable.btuong);
			}
			break;
		}
	}

	// --------------------- Method KiemTraChieuBi
	public static void KiemTraChieuBi() {
		boolean cuu = false;
		int tuong = 0, sy0 = 0, sy1 = 0, tinh0 = 0, tinh1 = 0, xe0 = 0, xe1 = 0, phao0 = 0, phao1 = 0, ma0 = 0, ma1 = 0, chot0 = 0, chot1 = 0, chot2 = 0, chot3 = 0, chot4 = 0;
		if (VanCo.state) // che do choi PvsP
		{
			if (VanCo.LuotDi == VanCo.n1.Phe) {
				if (ChieuTuong(VanCo.n1.qTuong) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < VanCo.n1.dsQuanCo.size(); k++) {
								if (VanCo.n1.dsQuanCo.get(k).TrangThai == 1) {
									if (VanCo.n1.dsQuanCo.get(k).KiemTra(i, j) == 1	&& VanCo.n1.dsQuanCo.get(k).TuongAnToan(i, j) == 1) {
										if (VanCo.n1.dsQuanCo.get(k).Ten == "tuong"	&& VanCo.n1.dsQuanCo.get(k).ThuTu == "0") tuong = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "sy" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") sy0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "sy" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") sy1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") tinh0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") tinh1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") xe0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") xe1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") phao0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") phao1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "ma" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0")	ma0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "ma" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1")	ma1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") chot0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") chot1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "2") chot2 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "3") chot3 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "4") chot4 = 1;
									}
								}else if(VanCo.n1.dsQuanCo.get(k).TrangThai == 2){
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "sy"){
										Sy tempSy = new Sy();
										tempSy.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempSy.KiemTra(i, j) == 1 && tempSy.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") sy0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") sy1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh"){
										Tinh tempTinh = new Tinh();
										tempTinh.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempTinh.KiemTra(i, j) == 1 && tempTinh.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") tinh0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") tinh1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe"){
										Xe tempXe = new Xe();
										tempXe.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempXe.KiemTra(i, j) == 1 && tempXe.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") xe0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") xe1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao"){
										Phao tempPhao = new Phao();
										tempPhao.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempPhao.KiemTra(i, j) == 1 && tempPhao.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") phao0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") phao1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma"){
										Ma tempMa = new Ma();
										tempMa.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempMa.KiemTra(i, j) == 1 && tempMa.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0")	ma0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1")	ma1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot"){
										Chot tempChot = new Chot();
										tempChot.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempChot.KiemTra(i, j) == 1 && tempChot.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") chot0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") chot1 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "2") chot2 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "3") chot3 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "4") chot4 = 1;
										}
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (sy1 == 1)
									|| (tinh0 == 1) || (tinh1 == 1)
									|| (xe0 == 1) || (xe1 == 1) || (phao0 == 1)
									|| (phao1 == 1) || (ma0 == 1) || (ma1 == 1)
									|| (chot0 == 1) || (chot1 == 1)
									|| (chot2 == 1) || (chot3 == 1)
									|| (chot4 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 1;
			} else {
				if (ChieuTuong(VanCo.n2.qTuong) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < VanCo.n2.dsQuanCo.size(); k++) {
								if (VanCo.n2.dsQuanCo.get(k).TrangThai == 1) {
									if (VanCo.n2.dsQuanCo.get(k).KiemTra(i, j) == 1	&& VanCo.n2.dsQuanCo.get(k).TuongAnToan(i, j) == 1) {
										if (VanCo.n2.dsQuanCo.get(k).Ten == "tuong"	&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")tuong = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "sy"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")sy0 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "sy"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "1")sy1 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "tinh"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")tinh0 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "tinh"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "1")tinh1 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "xe"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")xe0 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "xe"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "1")xe1 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "phao"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")phao0 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "phao"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "1")phao1 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "ma"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")ma0 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "ma"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "1")ma1 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "chot"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "0")chot0 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "chot"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "1")chot1 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "chot"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "2")chot2 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "chot"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "3")chot3 = 1;
										if (VanCo.n2.dsQuanCo.get(k).Ten == "chot"&& VanCo.n2.dsQuanCo.get(k).ThuTu == "4")chot4 = 1;
									}
								}else if(VanCo.n2.dsQuanCo.get(k).TrangThai == 2){
									if(VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "sy"){
										Sy tempSy = new Sy();
										tempSy.KhoiTao(VanCo.n2.dsQuanCo.get(k).Phe, VanCo.n2.dsQuanCo.get(k).Ten, VanCo.n2.dsQuanCo.get(k).ThuTu, VanCo.n2.dsQuanCo.get(k).TrangThai, VanCo.n2.dsQuanCo.get(k).Khoa, VanCo.n2.dsQuanCo.get(k).Hang, VanCo.n2.dsQuanCo.get(k).Cot);
										if(tempSy.KiemTra(i, j) == 1 && tempSy.TuongAnToan(i, j) == 1){
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "0") sy0 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "1") sy1 = 1;
										}
									}
									if(VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "tinh"){
										Tinh tempTinh = new Tinh();
										tempTinh.KhoiTao(VanCo.n2.dsQuanCo.get(k).Phe, VanCo.n2.dsQuanCo.get(k).Ten, VanCo.n2.dsQuanCo.get(k).ThuTu, VanCo.n2.dsQuanCo.get(k).TrangThai, VanCo.n2.dsQuanCo.get(k).Khoa, VanCo.n2.dsQuanCo.get(k).Hang, VanCo.n2.dsQuanCo.get(k).Cot);
										if(tempTinh.KiemTra(i, j) == 1 && tempTinh.TuongAnToan(i, j) == 1){
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "0") tinh0 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "1") tinh1 = 1;
										}
									}
									if(VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "xe"){
										Xe tempXe = new Xe();
										tempXe.KhoiTao(VanCo.n2.dsQuanCo.get(k).Phe, VanCo.n2.dsQuanCo.get(k).Ten, VanCo.n2.dsQuanCo.get(k).ThuTu, VanCo.n2.dsQuanCo.get(k).TrangThai, VanCo.n2.dsQuanCo.get(k).Khoa, VanCo.n2.dsQuanCo.get(k).Hang, VanCo.n2.dsQuanCo.get(k).Cot);
										if(tempXe.KiemTra(i, j) == 1 && tempXe.TuongAnToan(i, j) == 1){
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "0") xe0 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "1") xe1 = 1;
										}
									}
									if(VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "phao"){
										Phao tempPhao = new Phao();
										tempPhao.KhoiTao(VanCo.n2.dsQuanCo.get(k).Phe, VanCo.n2.dsQuanCo.get(k).Ten, VanCo.n2.dsQuanCo.get(k).ThuTu, VanCo.n2.dsQuanCo.get(k).TrangThai, VanCo.n2.dsQuanCo.get(k).Khoa, VanCo.n2.dsQuanCo.get(k).Hang, VanCo.n2.dsQuanCo.get(k).Cot);
										if(tempPhao.KiemTra(i, j) == 1 && tempPhao.TuongAnToan(i, j) == 1){
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "0") phao0 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "1") phao1 = 1;
										}
									}
									if(VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "ma"){
										Ma tempMa = new Ma();
										tempMa.KhoiTao(VanCo.n2.dsQuanCo.get(k).Phe, VanCo.n2.dsQuanCo.get(k).Ten, VanCo.n2.dsQuanCo.get(k).ThuTu, VanCo.n2.dsQuanCo.get(k).TrangThai, VanCo.n2.dsQuanCo.get(k).Khoa, VanCo.n2.dsQuanCo.get(k).Hang, VanCo.n2.dsQuanCo.get(k).Cot);
										if(tempMa.KiemTra(i, j) == 1 && tempMa.TuongAnToan(i, j) == 1){
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "0")	ma0 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "1")	ma1 = 1;
										}
									}
									if(VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "chot"){
										Chot tempChot = new Chot();
										tempChot.KhoiTao(VanCo.n2.dsQuanCo.get(k).Phe, VanCo.n2.dsQuanCo.get(k).Ten, VanCo.n2.dsQuanCo.get(k).ThuTu, VanCo.n2.dsQuanCo.get(k).TrangThai, VanCo.n2.dsQuanCo.get(k).Khoa, VanCo.n2.dsQuanCo.get(k).Hang, VanCo.n2.dsQuanCo.get(k).Cot);
										if(tempChot.KiemTra(i, j) == 1 && tempChot.TuongAnToan(i, j) == 1){
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "0") chot0 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "1") chot1 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "2") chot2 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "3") chot3 = 1;
											if (VanCo.n2.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n2.dsQuanCo.get(k).ThuTuChuaLat == "4") chot4 = 1;
										}
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (sy1 == 1)
									|| (tinh0 == 1) || (tinh1 == 1)
									|| (xe0 == 1) || (xe1 == 1) || (phao0 == 1)
									|| (phao1 == 1) || (ma0 == 1) || (ma1 == 1)
									|| (chot0 == 1) || (chot1 == 1)
									|| (chot2 == 1) || (chot3 == 1)
									|| (chot4 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 0;
			}
		} else { // che do choi PvsC
			if (VanCo.LuotDi == VanCo.n1.Phe) {
				if (ChieuTuong(VanCo.n1.qTuong) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < VanCo.n1.dsQuanCo.size(); k++) {
								if (VanCo.n1.dsQuanCo.get(k).TrangThai == 1) {
									if (VanCo.n1.dsQuanCo.get(k).KiemTra(i, j) == 1 && VanCo.n1.dsQuanCo.get(k).TuongAnToan(i, j) == 1) {
										if (VanCo.n1.dsQuanCo.get(k).Ten == "tuong"	&& VanCo.n1.dsQuanCo.get(k).ThuTu == "0")tuong = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "sy"&& VanCo.n1.dsQuanCo.get(k).ThuTu == "0")sy0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "sy"&& VanCo.n1.dsQuanCo.get(k).ThuTu == "1")sy1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "tinh"&& VanCo.n1.dsQuanCo.get(k).ThuTu == "0")	tinh0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1")tinh1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") xe0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") xe1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0")phao0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "phao"&& VanCo.n1.dsQuanCo.get(k).ThuTu == "1") phao1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "ma"&& VanCo.n1.dsQuanCo.get(k).ThuTu == "0")ma0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "ma"&& VanCo.n1.dsQuanCo.get(k).ThuTu == "1")ma1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "0") chot0 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "1") chot1 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "2") chot2 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "3") chot3 = 1;
										if (VanCo.n1.dsQuanCo.get(k).Ten == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTu == "4") chot4 = 1;
									}
								}else if(VanCo.n1.dsQuanCo.get(k).TrangThai == 2){
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "sy"){
										Sy tempSy = new Sy();
										tempSy.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempSy.KiemTra(i, j) == 1 && tempSy.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") sy0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") sy1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh"){
										Tinh tempTinh = new Tinh();
										tempTinh.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempTinh.KiemTra(i, j) == 1 && tempTinh.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") tinh0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") tinh1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe"){
										Xe tempXe = new Xe();
										tempXe.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempXe.KiemTra(i, j) == 1 && tempXe.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") xe0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") xe1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao"){
										Phao tempPhao = new Phao();
										tempPhao.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempPhao.KiemTra(i, j) == 1 && tempPhao.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") phao0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") phao1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma"){
										Ma tempMa = new Ma();
										tempMa.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempMa.KiemTra(i, j) == 1 && tempMa.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0")	ma0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1")	ma1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot"){
										Chot tempChot = new Chot();
										tempChot.KhoiTao(VanCo.n1.dsQuanCo.get(k).Phe, VanCo.n1.dsQuanCo.get(k).Ten, VanCo.n1.dsQuanCo.get(k).ThuTu, VanCo.n1.dsQuanCo.get(k).TrangThai, VanCo.n1.dsQuanCo.get(k).Khoa, VanCo.n1.dsQuanCo.get(k).Hang, VanCo.n1.dsQuanCo.get(k).Cot);
										if(tempChot.KiemTra(i, j) == 1 && tempChot.TuongAnToan(i, j) == 1){
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "0") chot0 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "1") chot1 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "2") chot2 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "3") chot3 = 1;
											if (VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.n1.dsQuanCo.get(k).ThuTuChuaLat == "4") chot4 = 1;
										}
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (sy1 == 1)
									|| (tinh0 == 1) || (tinh1 == 1)
									|| (xe0 == 1) || (xe1 == 1) || (phao0 == 1)
									|| (phao1 == 1) || (ma0 == 1) || (ma1 == 1)
									|| (chot0 == 1) || (chot1 == 1)
									|| (chot2 == 1) || (chot3 == 1)
									|| (chot4 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 0;
			} else {
				if (ChieuTuong(VanCo.computer.qTuong) == true) {
					for (int i = 0; i <= 9; i++) {
						for (int j = 0; j <= 8; j++) {
							for (int k = 0; k < VanCo.computer.dsQuanCo.size(); k++) {
								if (VanCo.computer.dsQuanCo.get(k).TrangThai == 1) {
									if (VanCo.computer.dsQuanCo.get(k).KiemTra(i, j) == 1 && VanCo.computer.dsQuanCo.get(k).TuongAnToan(i, j) == 1) {
										if (VanCo.computer.dsQuanCo.get(k).Ten == "tuong" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0")tuong = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "sy" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0")	sy0 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "sy" && VanCo.computer.dsQuanCo.get(k).ThuTu == "1") sy1 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "tinh" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0") tinh0 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "tinh" && VanCo.computer.dsQuanCo.get(k).ThuTu == "1") tinh1 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "xe" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0") xe0 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "xe" && VanCo.computer.dsQuanCo.get(k).ThuTu == "1") xe1 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "phao" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0") phao0 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "phao" && VanCo.computer.dsQuanCo.get(k).ThuTu == "1") phao1 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "ma" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0") ma0 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "ma" && VanCo.computer.dsQuanCo.get(k).ThuTu == "1") ma1 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTu == "0") chot0 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTu == "1") chot1 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTu == "2") chot2 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTu == "3") chot3 = 1;
										if (VanCo.computer.dsQuanCo.get(k).Ten == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTu == "4") chot4 = 1;
									}
								}else if(VanCo.computer.dsQuanCo.get(k).TrangThai == 2){
									if(VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "sy"){
										Sy tempSy = new Sy();
										tempSy.KhoiTao(VanCo.computer.dsQuanCo.get(k).Phe, VanCo.computer.dsQuanCo.get(k).Ten, VanCo.computer.dsQuanCo.get(k).ThuTu, VanCo.computer.dsQuanCo.get(k).TrangThai, VanCo.computer.dsQuanCo.get(k).Khoa, VanCo.computer.dsQuanCo.get(k).Hang, VanCo.computer.dsQuanCo.get(k).Cot);
										if(tempSy.KiemTra(i, j) == 1 && tempSy.TuongAnToan(i, j) == 1){
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "0") sy0 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "sy" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "1") sy1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "tinh"){
										Tinh tempTinh = new Tinh();
										tempTinh.KhoiTao(VanCo.computer.dsQuanCo.get(k).Phe, VanCo.computer.dsQuanCo.get(k).Ten, VanCo.computer.dsQuanCo.get(k).ThuTu, VanCo.computer.dsQuanCo.get(k).TrangThai, VanCo.computer.dsQuanCo.get(k).Khoa, VanCo.computer.dsQuanCo.get(k).Hang, VanCo.computer.dsQuanCo.get(k).Cot);
										if(tempTinh.KiemTra(i, j) == 1 && tempTinh.TuongAnToan(i, j) == 1){
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "0") tinh0 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "tinh" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "1") tinh1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "xe"){
										Xe tempXe = new Xe();
										tempXe.KhoiTao(VanCo.computer.dsQuanCo.get(k).Phe, VanCo.computer.dsQuanCo.get(k).Ten, VanCo.computer.dsQuanCo.get(k).ThuTu, VanCo.computer.dsQuanCo.get(k).TrangThai, VanCo.computer.dsQuanCo.get(k).Khoa, VanCo.computer.dsQuanCo.get(k).Hang, VanCo.computer.dsQuanCo.get(k).Cot);
										if(tempXe.KiemTra(i, j) == 1 && tempXe.TuongAnToan(i, j) == 1){
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "0") xe0 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "xe" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "1") xe1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "phao"){
										Phao tempPhao = new Phao();
										tempPhao.KhoiTao(VanCo.computer.dsQuanCo.get(k).Phe, VanCo.computer.dsQuanCo.get(k).Ten, VanCo.computer.dsQuanCo.get(k).ThuTu, VanCo.computer.dsQuanCo.get(k).TrangThai, VanCo.computer.dsQuanCo.get(k).Khoa, VanCo.computer.dsQuanCo.get(k).Hang, VanCo.computer.dsQuanCo.get(k).Cot);
										if(tempPhao.KiemTra(i, j) == 1 && tempPhao.TuongAnToan(i, j) == 1){
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "0") phao0 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "phao" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "1") phao1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "ma"){
										Ma tempMa = new Ma();
										tempMa.KhoiTao(VanCo.computer.dsQuanCo.get(k).Phe, VanCo.computer.dsQuanCo.get(k).Ten, VanCo.computer.dsQuanCo.get(k).ThuTu, VanCo.computer.dsQuanCo.get(k).TrangThai, VanCo.computer.dsQuanCo.get(k).Khoa, VanCo.computer.dsQuanCo.get(k).Hang, VanCo.computer.dsQuanCo.get(k).Cot);
										if(tempMa.KiemTra(i, j) == 1 && tempMa.TuongAnToan(i, j) == 1){
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "0")	ma0 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "ma" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "1")	ma1 = 1;
										}
									}
									if(VanCo.n1.dsQuanCo.get(k).TenQuanChuaLat == "chot"){
										Chot tempChot = new Chot();
										tempChot.KhoiTao(VanCo.computer.dsQuanCo.get(k).Phe, VanCo.computer.dsQuanCo.get(k).Ten, VanCo.computer.dsQuanCo.get(k).ThuTu, VanCo.computer.dsQuanCo.get(k).TrangThai, VanCo.computer.dsQuanCo.get(k).Khoa, VanCo.computer.dsQuanCo.get(k).Hang, VanCo.computer.dsQuanCo.get(k).Cot);
										if(tempChot.KiemTra(i, j) == 1 && tempChot.TuongAnToan(i, j) == 1){
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "0") chot0 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "1") chot1 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "2") chot2 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "3") chot3 = 1;
											if (VanCo.computer.dsQuanCo.get(k).TenQuanChuaLat == "chot" && VanCo.computer.dsQuanCo.get(k).ThuTuChuaLat == "4") chot4 = 1;
										}
									}
								}
							}
							if ((tuong == 1) || (sy0 == 1) || (sy1 == 1)
									|| (tinh0 == 1) || (tinh1 == 1)
									|| (xe0 == 1) || (xe1 == 1) || (phao0 == 1)
									|| (phao1 == 1) || (ma0 == 1) || (ma1 == 1)
									|| (chot0 == 1) || (chot1 == 1)
									|| (chot2 == 1) || (chot3 == 1)
									|| (chot4 == 1)) {
								cuu = true;
								break;
							}
						}
					}
				} else
					cuu = true;
				if (!cuu)
					winner = 1;
			}
		}
	}

	// --------------------- Method Undo
	public static synchronized void Undo() {
		int t;
		QuanCo temp_d, temp_c;
		temp_d = new QuanCo();
		temp_c = new QuanCo();
		VanCo.ply--;
		if (!VanCo.Marked){
			if (VanCo.GameLog.size() > 0) {
				// Kiem tra, tham chieu den quan co Dau trong nuoc di
				if (VanCo.state) {
					if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Phe == VanCo.n1.Phe) {
						for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
							if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu)
								temp_d = VanCo.n1.dsQuanCo.get(i);
						}
					} else {
						for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
							if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Ten == VanCo.n2.dsQuanCo.get(i).Ten	&& VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.ThuTu == VanCo.n2.dsQuanCo.get(i).ThuTu)
								temp_d = VanCo.n2.dsQuanCo.get(i);
						}
					}
				} else {
					if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Phe == VanCo.n1.Phe) {
						for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
							if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu)
								temp_d = VanCo.n1.dsQuanCo.get(i);
						}
					} else {
						for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
							if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Ten == VanCo.computer.dsQuanCo.get(i).Ten && VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.ThuTu == VanCo.computer.dsQuanCo.get(i).ThuTu)
								temp_d = VanCo.computer.dsQuanCo.get(i);
						}
					}
				}
				// Kiem tra xem co phai la nuoc di an quan doi phuong ko
				Log.d("undo", VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Ten 
						+ "/" + VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.TrangThai + "/"
						+ "/" + VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten + "/"
						+ VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.TrangThai + "/");
				if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten == "")	t = 0;
				else t = 1;

				switch (t) {
				// Neu la nuoc di khong an quan co cua doi phuong
				case 0:
					// Tra lai o co trong 
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].Trong = true;
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].Phe = 2;
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].Ten = "";
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].ThuTu = "";
				
					// Ä�áº·t quĂ¢n cá»� vá»«a Ä‘i trá»Ÿ láº¡i vá»‹ trĂ­ cÅ©
					temp_d.Hang = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Hang_Dau;
					temp_d.Cot = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cot_Dau;
					temp_d.TrangThai = VanCo.GameLog.get(VanCo.GameLog.size() -1).TrangThaiDau;
					//Dat lai trang thai cho quan co - chi o che do PvP
					if(VanCo.state){
						if (temp_d.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu){
									VanCo.n1.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.n1.dsQuanCo.get(i).drawImage();
								}
							}
						} else {
							for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.n2.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.n2.dsQuanCo.get(i).ThuTu){
									VanCo.n2.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.n2.dsQuanCo.get(i).drawImage();
								}
							}
						}
					}else{
						if (temp_d.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu){
									VanCo.n1.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.n1.dsQuanCo.get(i).drawImage();
								}
							}
						} else {
							for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.computer.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.computer.dsQuanCo.get(i).ThuTu){
									VanCo.computer.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.computer.dsQuanCo.get(i).drawImage();
								}
							}
						}
					}
					
					temp_d.drawImage();
					GameActivity.setLayout(temp_d.imgUnit, temp_d.Cot * 51 + 10,temp_d.Hang * 70  + 98, 0, 0);
					// Thiet lap quan co vao vi tri tren ban co
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].Trong = false;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].Phe = temp_d.Phe;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].ThuTu = temp_d.ThuTu;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].Ten = temp_d.Ten;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].TrangThai = temp_d.TrangThai;
					// Xoa nuoc di cuoi cung khoi gamelog
					VanCo.GameLog.remove(VanCo.GameLog.size() - 1);
					if (VanCo.winner != 2)
						VanCo.winner = 2;
					VanCo.KiemTraChieuTuong();
					// Tra lai luot di
					if(VanCo.state)
						VanCo.DoiLuotDi();
					else{
						if(VanCo.GameLog.size() == 0)
							VanCo.DoiLuotDi();
					}
					break;

				// Neu la nuoc di an quan co cua doi phuong
				case 1:
					// Kiá»ƒm tra -> tham chiáº¿u temp_c Ä‘áº¿n quĂ¢n cá»� trĂªn bĂ n cá»�
					if (VanCo.state) {
						if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten == VanCo.n1.dsQuanCo.get(i).Ten && VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu)
									temp_c = VanCo.n1.dsQuanCo.get(i);
							}
						} else {
							for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
								if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten == VanCo.n2.dsQuanCo.get(i).Ten && VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.ThuTu == VanCo.n2.dsQuanCo.get(i).ThuTu)
									temp_c = VanCo.n2.dsQuanCo.get(i);
							}
						}
					} else {
						if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten == VanCo.n1.dsQuanCo.get(i).Ten && VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu)
									temp_c = VanCo.n1.dsQuanCo.get(i);
							}
						} else {
							for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
								if (VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten == VanCo.computer.dsQuanCo.get(i).Ten && VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.ThuTu == VanCo.computer.dsQuanCo.get(i).ThuTu)
									temp_c = VanCo.computer.dsQuanCo.get(i);
							}
						}
					}
					// Thiáº¿t láº­p láº¡i quĂ¢n cá»� á»Ÿ vá»‹ trĂ­ vá»«a bá»‹ Äƒn trĂªn BĂ n Cá»�
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].Trong = false;
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].Phe = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Phe;
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].Ten = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.Ten;
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].ThuTu = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.ThuTu;
					BanCo.ViTri[VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Hang][VanCo.GameLog.get(VanCo.GameLog.size() - 1).Dau.Cot].TrangThai = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cuoi.TrangThai;
					// Ä�áº·t quĂ¢n cá»� bá»‹ Äƒn vĂ o vá»‹ trĂ­ á»Ÿ trĂªn
					temp_c.TrangThai = VanCo.GameLog.get(VanCo.GameLog.size() -1).TrangThaiCuoi;
					temp_c.imgUnit.setVisibility(View.VISIBLE);
					temp_c.drawImage();
					//Dat lai trang thai cho quan co - chi o che do PvP
					if(VanCo.state){
						if (temp_c.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (temp_c.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& temp_c.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu){
									VanCo.n1.dsQuanCo.get(i).TrangThai = temp_c.TrangThai;
									VanCo.n1.dsQuanCo.get(i).drawImage();
								}
							}
						} else {
							for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
								if (temp_c.Ten == VanCo.n2.dsQuanCo.get(i).Ten	&& temp_c.ThuTu == VanCo.n2.dsQuanCo.get(i).ThuTu){
									VanCo.n2.dsQuanCo.get(i).TrangThai = temp_c.TrangThai;
									VanCo.n2.dsQuanCo.get(i).drawImage();
								}
							}
						}
					}else{
						if (temp_c.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (temp_c.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& temp_c.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu){
									VanCo.n1.dsQuanCo.get(i).TrangThai = temp_c.TrangThai;
									VanCo.n1.dsQuanCo.get(i).drawImage();
								}
							}
						} else {
							for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
								if (temp_c.Ten == VanCo.computer.dsQuanCo.get(i).Ten	&& temp_c.ThuTu == VanCo.computer.dsQuanCo.get(i).ThuTu){
									VanCo.computer.dsQuanCo.get(i).TrangThai = temp_c.TrangThai;
									VanCo.computer.dsQuanCo.get(i).drawImage();
								}
							}
						}
					}
					
					GameActivity.setLayout(temp_c.imgUnit, temp_c.Cot * 51 + 10,temp_c.Hang * 70  + 98, 0, 0);
					// Ä�áº·t quĂ¢n cá»� vá»«a Ä‘i trá»Ÿ láº¡i vá»‹ trĂ­ cÅ©
					temp_d.Hang = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Hang_Dau;
					temp_d.Cot = VanCo.GameLog.get(VanCo.GameLog.size() - 1).Cot_Dau;
					temp_d.TrangThai = VanCo.GameLog.get(VanCo.GameLog.size() -1).TrangThaiDau;
					temp_d.imgUnit.setVisibility(View.VISIBLE);
					temp_d.drawImage();
					//Dat lai trang thai cho quan co - chi o che do PvP
					if(VanCo.state){
						if (temp_d.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu){
									VanCo.n1.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.n1.dsQuanCo.get(i).drawImage();
								}
							}
						} else {
							for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.n2.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.n2.dsQuanCo.get(i).ThuTu){
									VanCo.n2.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.n2.dsQuanCo.get(i).drawImage();
								}
							}
						}
					}else{
						if (temp_d.Phe == VanCo.n1.Phe) {
							for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.n1.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.n1.dsQuanCo.get(i).ThuTu){
									VanCo.n1.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.n1.dsQuanCo.get(i).drawImage();
								}
							}
						} else {
							for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
								if (temp_d.Ten == VanCo.computer.dsQuanCo.get(i).Ten	&& temp_d.ThuTu == VanCo.computer.dsQuanCo.get(i).ThuTu){
									VanCo.computer.dsQuanCo.get(i).TrangThai = temp_d.TrangThai;
									VanCo.computer.dsQuanCo.get(i).drawImage();
								}
							}
						}
					}
					
					GameActivity.setLayout(temp_d.imgUnit, temp_d.Cot * 51 + 10,temp_d.Hang * 70  + 98, 0, 0);
					// Thiet lap quan co vao vi tri tren ban co
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].Trong = false;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].Phe = temp_d.Phe;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].ThuTu = temp_d.ThuTu;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].Ten = temp_d.Ten;
					BanCo.ViTri[temp_d.Hang][temp_d.Cot].TrangThai = temp_d.TrangThai;
					// Xoa nuoc di cuoi cung khoi gamelog
					VanCo.GameLog.remove(VanCo.GameLog.size() - 1);
					// Tráº£ láº¡i VanCo.winner=2 náº¿u nÆ°á»›c Ä‘i Äƒn TÆ°á»›ng Ä‘á»‘i phÆ°Æ¡ng
					if (VanCo.winner != 2)
						VanCo.winner = 2;
					// Kiem tra chieu tuong
					// if (VanCo.AmThanh) ClickSound("0");
					VanCo.KiemTraChieuTuong();
					// Tra lai luot di
					if(VanCo.state)
						VanCo.DoiLuotDi();
					else{
						if(VanCo.GameLog.size() == 0)
							VanCo.DoiLuotDi();
					}
					break;
				}
			}
		}
	}

	// --------------------- Method DoiLuotDi
	public static void DoiLuotDi() {
		if (VanCo.LuotDi == 0){
			VanCo.LuotDi = 1;
			GameActivity.imgTurn.setImageResource(R.drawable.btuong_turn);
		}
		else{
			VanCo.LuotDi = 0;
			GameActivity.imgTurn.setImageResource(R.drawable.rtuong_turn);
		}
		if (VanCo.state) {
			if (VanCo.LuotDi == VanCo.n1.Phe) {
				for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
					VanCo.n2.dsQuanCo.get(i).Khoa = true;
					VanCo.n2.dsQuanCo.get(i).imgUnit.setEnabled(false);
				}

				for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
					VanCo.n1.dsQuanCo.get(i).Khoa = false;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setEnabled(true);
				}
			} else {
				for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
					VanCo.n1.dsQuanCo.get(i).Khoa = true;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setEnabled(false);
				}

				for (int i = 0; i < VanCo.n2.dsQuanCo.size(); i++) {
					VanCo.n2.dsQuanCo.get(i).Khoa = false;
					VanCo.n2.dsQuanCo.get(i).imgUnit.setEnabled(true);
				}
			}
		} else {
			if (VanCo.LuotDi == VanCo.n1.Phe) {
				for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
					VanCo.computer.dsQuanCo.get(i).Khoa = true;
					VanCo.computer.dsQuanCo.get(i).imgUnit.setEnabled(false);
				}

				for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
					VanCo.n1.dsQuanCo.get(i).Khoa = false;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setEnabled(true);
				}
			} else {
				for (int i = 0; i < VanCo.computer.dsQuanCo.size(); i++) {
					VanCo.computer.dsQuanCo.get(i).Khoa = true;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setEnabled(false);
				}

				for (int i = 0; i < VanCo.n1.dsQuanCo.size(); i++) {
					VanCo.n1.dsQuanCo.get(i).Khoa = true;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setEnabled(false);
				}
			}
		}
	}

	// --------------------- Method DatQuanCo
	public static void DatQuanCo(Object sender, QuanCo q, int i, int j) {
		if (sender.equals(BanCo.ViTri[i][j].imgMove)) {
			q.Hang = i;
			q.Cot = j;
			if(q.isFirst){
				q.TrangThai = 1;
				q.isFirst = false;
			}
			else
				q.TrangThai = q.TrangThai;
			q.draw();
		}
		if (sender instanceof QuanCo) {
			BanCo.ViTri[i][j].Trong = false;
			BanCo.ViTri[i][j].Phe = VanCo.DanhDau.Phe;
			BanCo.ViTri[i][j].Ten = VanCo.DanhDau.Ten;
			BanCo.ViTri[i][j].ThuTu = VanCo.DanhDau.ThuTu;
			BanCo.ViTri[i][j].TrangThai = VanCo.DanhDau.TrangThai;
			VanCo.DanhDau.Hang = i;
			VanCo.DanhDau.Cot = j;
			GameActivity.setLayout(VanCo.DanhDau.imgUnit, j * 25 + 7,i * 26 + 35, 0, 0);
		}
	}

	// -----------------------Method AnQuanCo
	public static void AnQuanCo(QuanCo q) {
		q.TrangThai = 0;
		q.imgUnit.setVisibility(ImageView.INVISIBLE);
		if (q.Phe == 1) {
			VanCo.QuanDenBiAn.add(q);
		} else {
			VanCo.QuanDoBiAn.add(q);
		}
	}

	// -----------------------Method LuuVaoGameLog
	public static void LuuVaoGameLog(Object sender, QuanCo q) {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 8; j++) {
				if (sender.equals(BanCo.ViTri[i][j].imgMove)) {
					if (BanCo.ViTri[i][j].Trong) {
						NuocDi newND = new NuocDi();
						newND.Dau = VanCo.DanhDau;
						newND.TrangThaiDau = q.TrangThai;
						newND.Hang_Dau = q.Hang;
						newND.Cot_Dau = q.Cot;
						newND.Dau.TrangThai = VanCo.DanhDau.TrangThai;
						newND.Cuoi = new QuanCo();
						newND.TrangThaiCuoi = 0;
						Log.d("stt",newND.Dau.Ten + "/" + newND.TrangThaiDau + "/" + newND.Cuoi.Ten + "/" + newND.TrangThaiCuoi);
						GameLog.add(newND);
					} else {
						NuocDi newND = new NuocDi();
						newND.Dau = VanCo.DanhDau;
						newND.TrangThaiDau = VanCo.DanhDau.TrangThai;
						newND.Hang_Dau = VanCo.DanhDau.Hang;
						newND.Cot_Dau = VanCo.DanhDau.Cot;
						newND.Dau.TrangThai = VanCo.DanhDau.TrangThai;
						newND.Cuoi = q;
						newND.TrangThaiCuoi = q.TrangThai;
						newND.Hang_Cuoi = q.Hang;
						newND.Cot_Cuoi = q.Cot;
						newND.Cuoi.TrangThai = q.TrangThai;
						Log.d("stt",newND.Dau.Ten + "/" + newND.TrangThaiDau + "/" + newND.Cuoi.Ten + "/" + newND.TrangThaiCuoi);
						GameLog.add(newND);
					}
				}
			}
		}
	}
	//--------------------------Method Save
	public static synchronized void save() {
		try {
			FileWriter fileWriter = new FileWriter("/data/data/hut.game/files/resume.txt");
			BufferedWriter out = new BufferedWriter(fileWriter);
			out.write(Integer.toString(VanCo.LuotDi));
			out.newLine();
			// tráº¡ng thĂ¡i
			if (VanCo.state) {
				out.write("1");
				out.newLine();
			}
			else{
				out.write("0");
				out.newLine();
			}
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (BanCo.ViTri[i][j].Trong == false) {
						out.write((Integer.toString(BanCo.ViTri[i][j].Hang)
								+ Integer.toString(BanCo.ViTri[i][j].Cot)
								+ Integer.toString(BanCo.ViTri[i][j].Phe)
								+ Integer.toString(BanCo.ViTri[i][j].TrangThai)
								+ (BanCo.ViTri[i][j].ThuTu) + (BanCo.ViTri[i][j].Ten)));
						out.newLine();
					}

				}
			}
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//--------------------------Method Resume
	public static synchronized void Resume() {
		int hang, cot, phe, luotdi, state1,trangthai;
		String ten = "", thutu = "";
		try {
			FileReader fileReader = new FileReader("/data/data/hut.game/files/resume.txt");
			BufferedReader br = new BufferedReader(fileReader);

			// Ä�á»�c vĂ o giĂ¡ trá»‹ luotdi
			luotdi = Integer.valueOf(br.readLine());
			// Ä�á»�c vĂ o tĂªn tráº¡ng thĂ¡i
			state1 = Integer.valueOf(br.readLine());
			if(state1 == 1)
			{
				VanCo.state = true;
				VanCo.n1 = new Player(0,GameActivity.mOnTouch);
				for(int i = 0; i < VanCo.n1.dsQuanCo.size();i++)
				{
					VanCo.n1.dsQuanCo.get(i).TrangThai = 0;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.INVISIBLE);
				}
				VanCo.n2 = new Player(1,GameActivity.mOnTouch);
				for(int i = 0; i < VanCo.n2.dsQuanCo.size();i++)
				{
					VanCo.n2.dsQuanCo.get(i).TrangThai = 0;
					VanCo.n2.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.INVISIBLE);
				}
			}else{
				VanCo.state = false;
				VanCo.n1 = new Player(1,GameActivity.mOnTouch);
				for(int i = 0; i < VanCo.n1.dsQuanCo.size();i++)
				{
					VanCo.n1.dsQuanCo.get(i).TrangThai = 0;
					VanCo.n1.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.INVISIBLE);
				}
				VanCo.computer = new Computer(0);
				for(int i = 0; i < VanCo.computer.dsQuanCo.size();i++)
				{
					VanCo.computer.dsQuanCo.get(i).TrangThai = 0;
					VanCo.computer.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.INVISIBLE);
				}
			}
			BanCo.ResetBanCo();
			String line = "";
			while ((line = br.readLine()) != null) {
				hang = Integer.parseInt(String.valueOf(line.charAt(0)));
				cot = Integer.parseInt(String.valueOf(line.charAt(1)));
				phe = Integer.parseInt(String.valueOf(line.charAt(2)));
				trangthai = Integer.parseInt(String.valueOf(line.charAt(3)));
				thutu = String.valueOf(line.charAt(4));
				for (int i = 5; i < line.length(); i++) {
					ten += String.valueOf(line.charAt(i));
				}
				// Kiá»ƒm tra quĂ¢n cá»� Ä‘á»ƒ tham chiáº¿u
				if (state1 == 1) {
					if (phe == 0)
					{
						for(int i = 0; i < VanCo.n1.dsQuanCo.size(); i++)
						{
							if(ten.equals(VanCo.n1.dsQuanCo.get(i).Ten) && thutu.equals(VanCo.n1.dsQuanCo.get(i).ThuTu))
							{
								VanCo.n1.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.VISIBLE);
								if (luotdi != phe) VanCo.n1.dsQuanCo.get(i).Khoa = true;
				                else VanCo.n1.dsQuanCo.get(i).Khoa = false;
								VanCo.n1.dsQuanCo.get(i).Hang = hang;
								VanCo.n1.dsQuanCo.get(i).Cot = cot;
								VanCo.n1.dsQuanCo.get(i).TrangThai = trangthai;
								VanCo.n1.dsQuanCo.get(i).draw();
							}
						}
							
					}
					if (phe == 1)
					{
						for(int i = 0; i < VanCo.n2.dsQuanCo.size(); i++)
						{
							if(ten.equals(VanCo.n2.dsQuanCo.get(i).Ten) && thutu.equals(VanCo.n2.dsQuanCo.get(i).ThuTu))
							{
								VanCo.n2.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.VISIBLE);
								if (luotdi != phe) VanCo.n2.dsQuanCo.get(i).Khoa = true;
				                else VanCo.n2.dsQuanCo.get(i).Khoa = false;
								VanCo.n2.dsQuanCo.get(i).Hang = hang;
								VanCo.n2.dsQuanCo.get(i).Cot = cot;
								VanCo.n2.dsQuanCo.get(i).TrangThai = trangthai;
								VanCo.n2.dsQuanCo.get(i).draw();
							}
						}
					}
				} else {
					if (phe == 0)
					{
						for(int i = 0; i < VanCo.computer.dsQuanCo.size(); i++)
						{
							if(ten.equals(VanCo.computer.dsQuanCo.get(i).Ten) && thutu.equals(VanCo.computer.dsQuanCo.get(i).ThuTu))
							{
								VanCo.computer.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.VISIBLE);
								if (luotdi != phe) VanCo.computer.dsQuanCo.get(i).Khoa = true;
				                else VanCo.computer.dsQuanCo.get(i).Khoa = false;
								VanCo.computer.dsQuanCo.get(i).Hang = hang;
								VanCo.computer.dsQuanCo.get(i).Cot = cot;
								VanCo.computer.dsQuanCo.get(i).TrangThai = trangthai;
								VanCo.computer.dsQuanCo.get(i).draw();
							}
						}
					}
					if (phe == 1)
					{
						for(int i = 0; i < VanCo.n1.dsQuanCo.size(); i++)
						{
							if(ten.equals(VanCo.n1.dsQuanCo.get(i).Ten) && thutu.equals(VanCo.n1.dsQuanCo.get(i).ThuTu))
							{
								VanCo.n1.dsQuanCo.get(i).imgUnit.setVisibility(ImageView.VISIBLE);
								if (luotdi != phe) VanCo.n1.dsQuanCo.get(i).Khoa = true;
				                else VanCo.n1.dsQuanCo.get(i).Khoa = false;
								VanCo.n1.dsQuanCo.get(i).Hang = hang;
								VanCo.n1.dsQuanCo.get(i).Cot = cot;
								VanCo.n1.dsQuanCo.get(i).TrangThai = trangthai;
								VanCo.n1.dsQuanCo.get(i).draw();
							}
						}
					}
				}
				// Tráº£ láº¡i giĂ¡ trá»‹ null Ä‘á»ƒ láº¥y thĂ´ng tin quĂ¢n cá»� khĂ¡c
				ten = "";
				thutu = "";
				line = "";
			}
			VanCo.LuotDi = luotdi;
			VanCo.turn = 0;
			VanCo.DangChoi = true;
			VanCo.winner = 2;
//			for(int i = 0; i <= 9; i++)
//			{
//				for(int j = 0; j <= 8; j++)
//				{
//					Log.d("BanCoRe","[Hang,Cot]: " + i + " " + j +" Ten: " + BanCo.ViTri[i][j].Ten + "Phe: " + BanCo.ViTri[i][j].Phe);
//				}
//			}
			// Kiá»ƒm tra chiáº¿u tÆ°á»›ng
			VanCo.KiemTraChieuTuong();
			br.close();

		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
